<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户编号" prop="custom_code">
                        <el-input v-model="custom_code" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="开票类型" prop="open_type">
                        <el-select v-model="form.open_type" placeholder="请选择开票类型" style="width:100%" clearable>
                            <!-- <el-option label="增值税纸质专票" :value="1"/>
                            <el-option label="增值税纸质普票" :value="2"/> -->
                            <el-option label="增值税电子普票" :value="3"/>
                            <el-option label="增值税电子专票" :value="4"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="客户抬头" prop="customer_rise">
                        <el-input v-model="form.customer_rise" placeholder="请输入客户抬头" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="抬头类型" prop="rise_type">
                        <el-select v-model="form.rise_type" placeholder="请选择开票类型" style="width:100%" clearable>
                            <el-option label="个人" :value="0"/>
                            <el-option label="单位" :value="1"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="纳税人识别号" prop="taxpayer_num">
                        <el-input v-model="form.taxpayer_num" placeholder="请输入纳税人识别号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="开户行" prop="open_bank">
                        <el-input v-model="form.open_bank" placeholder="请输入开户行" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="开户账号" prop="open_bank_account">
                        <el-input v-model="form.open_bank_account" placeholder="请输入开户账号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="开票地址" prop="open_address">
                        <el-input v-model="form.open_address" placeholder="请输入开票地址" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <!-- <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="form.phone" placeholder="请输入电话" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮箱" prop="email">
                        <el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="收件人" prop="receipt">
                        <el-input v-model="form.receipt" placeholder="请输入收件人" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮寄地址" prop="mail_address">
                        <el-input v-model="form.mail_address" placeholder="请输入邮寄地址" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="form.mobile" placeholder="请输入手机号" clearable></el-input>
                </el-form-item>
            </el-col> -->
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增开票',
				edit: '编辑开票',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
				customer_id: "",
				open_type: "",
				customer_rise: "",
				rise_type: "",
				taxpayer_num: "",
				open_bank: "",
				open_bank_account: "",
				open_address: "",
				phone: "",
				email: "",
				receipt: "",
				mail_address: "",
				mobile: "",
			},
            custom_code:"",
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.client.invoice.edit.post(this.form);
					} else {
						res = await this.$API.client.invoice.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        setId(data){
            this.form.customer_id = data.id
            this.custom_code = data.custom_code
        },
		//表单注入数据
		setData(data) {
            this.form.id = data.id
            this.form.customer_id = data.customer_id
            this.form.open_type = data.open_type
            this.form.customer_rise = data.customer_rise
            this.form.rise_type = data.rise_type
            this.form.taxpayer_num = data.taxpayer_num
            this.form.open_bank = data.open_bank
            this.form.open_bank_account = data.open_bank_account
            this.form.open_address = data.open_address
            this.form.phone = data.phone
            this.form.email = data.email
            this.form.receipt = data.receipt
            this.form.mail_address = data.mail_address
            this.form.mobile = data.mobile
            this.custom_code = data.customer.apply_code
		},
	}
}
</script>

<style></style>
